package com.ncmem.up7.database.oracle;

import com.ncmem.model.FileInf;
import com.ncmem.up7.database.DbHelper;
import com.ncmem.up7.database.sql.SqlFile;
import com.ncmem.up7.sql.SqlExec;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OracleFile extends SqlFile {

    public boolean read(FileInf m)
    {
        boolean ret = false;
        StringBuilder sb = new StringBuilder();
        sb.append("select");
        sb.append(" f_pathSvr");//1
        sb.append(",f_pathRel");//2
        sb.append(",f_md5");//3
        sb.append(",f_lenLoc");//4
        sb.append(",f_sizeLoc");//5
        sb.append(",f_pos");//6
        sb.append(",f_blockCount");//7
        sb.append(",f_blockSize");//8
        sb.append(",f_blockPath");//9
        sb.append(",f_lenSvr");//10
        sb.append(",f_pid");//11
        sb.append(",f_perSvr");//12
        sb.append(",f_complete");//13
        sb.append(",f_time");//14
        sb.append(",f_deleted");//15
        sb.append(",f_merged");//16
        sb.append(",f_pidRoot");//17
        sb.append(",f_fdTask");//18
        sb.append(",f_fdChild");//19
        sb.append(",f_uid");//20
        sb.append(",f_nameLoc");//21
        sb.append(",f_nameSvr");//22
        sb.append(",f_pathLoc");//23
        sb.append(" from up7_files where f_id=? and rownum<=1");

        DbHelper db = new DbHelper();
        PreparedStatement cmd = db.GetCommand(sb.toString());
        try {
            cmd.setString(1, m.id);
            ResultSet r = db.ExecuteDataSet(cmd);
            if(r.next())
            {
                m.pathSvr 		= r.getString(1);
                m.pathRel 		= r.getString(2);
                m.md5			= r.getString(3);
                m.lenLoc		= r.getLong(4);
                m.sizeLoc		= r.getString(5);
                m.blockCount 	= r.getInt(7);
                m.blockSize  	= r.getInt(8);
                m.blockPath  	= r.getString(9);
                m.lenSvr     	= r.getLong(10);
                m.pid        	= r.getString(11);
                m.perSvr     	= r.getString(12);
                m.complete   	= r.getBoolean(13);
                m.time = r.getDate(14);
                m.deleted    	= r.getBoolean(15);
                //m.merged   	= r.getBoolean(16);
                m.pidRoot    	= r.getString(17);
                m.fdTask     	= r.getBoolean(18);
                m.fdChild  	= r.getBoolean(19);
                m.uid        	= r.getString(20);
                m.nameLoc    	= r.getString(21);
                m.nameSvr    	= r.getString(22);
                m.pathLoc    	= r.getString(23);
                ret = true;
            }
            r.close();
            cmd.getConnection().close();
            cmd.close();
        } catch (SQLException e) {e.printStackTrace();}
        return ret;
    }

    /**
     * 清空文件表，文件夹表数据。
     */
    public void Clear()
    {
        DbHelper db = new DbHelper();
        db.ExecuteNonQuery("truncate table up7_files");
        db.ExecuteNonQuery("truncate table up7_folders");
    }

    /**
     * 更新子文件路径
     * @param pathRelOld
     * @param pathRelNew
     */
    public void updatePathRel(String pathRelOld,String pathRelNew)
    {
        //更新子文件路径
        String sql = String.format("update up7_files set f_pathRel=REPLACE(f_pathRel,'%s/','%s/') where instr(f_pathRel,'%s/')=1",
                pathRelOld,
                pathRelNew,
                pathRelOld
        );

        SqlExec se = new SqlExec();
        se.exec(sql);

    }
}